package com.right.health.app.ui.household.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.magic.basic.utils.CollectionUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.BloodPressureResponse;
import com.right.health.app.net.HouseholdApi;
import com.right.health.app.ui.household.BloodPressureActivity;
import com.right.health.app.utils.LineChartUtil;
import com.right.health.app.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Created by hopeliao on 2019-12-30
 *
 */
public class BloodPressureFragment3 extends BasePresenterFragment implements OnHttpListener {
    private  String[] mValues ;

    private LineChart mLineChart;

    private HouseholdApi mHouseholdApi;

    private BloodPressureResponse mResponse;
    private Map<String, Float> mXAxisMap;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.blood_pressure_fragment, viewGroup, false);
    }

    @Override
    public void findView() {
        mLineChart = (LineChart) findViewById(R.id.lineChart);
        LineChartUtil.setupLineChart(mLineChart);
    }

    @Override
    public void initialize() {
        mHouseholdApi = new HouseholdApi();
        mHouseholdApi.setListener(this);

        mHouseholdApi.addRequestCode(BloodPressureActivity.HTTP_CDOE_TYPE_TYPE);
        mHouseholdApi.getBloodPressureByType(BloodPressureResponse.TYPE_MONTH);
        LineChartUtil.getCommonXAxis(mLineChart, 6, 30, new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if (value>0) {
                    return mValues[(int) value - 1];
                }
                return mValues[(int)value];
            }
        });
        mValues = TimeUtils.getPastMonth();
        mXAxisMap=new HashMap<>();
        for (int i = 0; i <mValues.length ; i++) {
            mXAxisMap.put(mValues[i], (float)i+1 );
        }
    }

    @Override
    public void refreshData() {
        super.refreshData();
        if (mHouseholdApi==null) {
            mHouseholdApi = new HouseholdApi();
            mHouseholdApi.setListener(this);
        }
        mHouseholdApi.addRequestCode(BloodPressureActivity.HTTP_CDOE_TYPE_TYPE);
        mHouseholdApi.getBloodPressureByType(BloodPressureResponse.TYPE_MONTH);
    }

    private void refreshLineChat() {
        if (mResponse != null && !CollectionUtil.isEmpty(mResponse.bloodpressures)) {

            double maxValue = 0;
            List<List<Entry>> mLineValues = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                mLineValues.add(new ArrayList<>());
            }
            Collections.sort(mResponse.bloodpressures, new Comparator<BloodPressureResponse.BloodPressureInfo>() {
                @Override
                public int compare(BloodPressureResponse.BloodPressureInfo t, BloodPressureResponse.BloodPressureInfo t1) {
                    if (t1.createtime == null || t.createtime == null) {
                        return -1;
                    }
                    return (Float.valueOf(t.createtime) > Float.valueOf(t1.createtime)) ? 1 : -1;
                }
            });
            int length = TimeUtils.getDayNumInMonth();
            for (BloodPressureResponse.BloodPressureInfo info : mResponse.bloodpressures) {
                if (info==null||info.createtime == null ||info.highpressure==null) {
                    continue;
                }
                String key =TimeUtils.getMonthAndDay(Long.valueOf(info.createtime));
                float targetX = 0;
                try {
                    if (mXAxisMap.containsKey(key)) {
                        targetX = mXAxisMap.get(key);
                    }else {
                        continue;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                float highpressure = Float.valueOf(info.highpressure);
                float lowvoltage = Float.valueOf(info.lowvoltage);
                float pulse = Float.valueOf(info.pulse);

                maxValue = Math.max(maxValue, Math.ceil(highpressure));
                maxValue = Math.max(maxValue, Math.ceil(lowvoltage));
                maxValue = Math.max(maxValue, Math.ceil(pulse));
                mLineValues.get(0).add(new Entry(targetX, highpressure));
                mLineValues.get(1).add(new Entry(targetX, lowvoltage));
                mLineValues.get(2).add(new Entry(targetX, pulse));
            }

            LineDataSet hpLine = LineChartUtil.createLine(mLineValues.get(0), getResources().getColor(R.color.color_09C3BE));
            LineDataSet lpLine = LineChartUtil.createLine(mLineValues.get(1), getResources().getColor(R.color.color_EDAD22));
            LineDataSet plLine = LineChartUtil.createLine(mLineValues.get(2), getResources().getColor(R.color.color_229EED));
            LineChartUtil.getCommonYAxis(mLineChart, 5, (int) Math.ceil(maxValue), null);
            LineChartUtil.refresh(mLineChart, hpLine,lpLine,plLine);
        } else {
            LineChartUtil.refresh(mLineChart);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (request.requestCode == BloodPressureActivity.HTTP_CDOE_TYPE_TYPE) {
            String currType = String.valueOf(request.tag);
            if (response.targetData instanceof BloodPressureResponse) {
                mResponse = response.cast(BloodPressureResponse.class);
                refreshLineChat();
            }
        }
    }
}
